package  cn.klaw.config;

import com.baomidou.lock.LockTemplate;
import cn.klaw.consts.ProjectEventConst;
import  cn.klaw.handler.ProjectEventMQMessageHandler;
import  cn.klaw.handler.ProjectEventMQSender;
import cn.klaw.properties.EventProperty;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.cloud.stream.function.StreamBridge;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.Message;

import java.util.function.Consumer;

@Configuration(proxyBeanMethods = false)
public class MqAutoConfiguration {

    @Bean(ProjectEventConst.DEFAULT_TOPIC_NAME)
    @ConditionalOnProperty(prefix = "event", name = "event-publish-type", havingValue = ProjectEventConst.EVENT_PUBLISH_TYPE_QUEUE)
    public Consumer<Message<?>> getDefaultMqConsumer(LockTemplate lockTemplate){
        return new ProjectEventMQMessageHandler(lockTemplate);
    }

    /**
     * 自定义事件发布渠道为QUEUE
     */
    @Bean
    @ConditionalOnProperty(prefix = "event", name = "event-publish-type", havingValue = ProjectEventConst.EVENT_PUBLISH_TYPE_QUEUE)
    public ProjectEventMQSender getProjectEventMQSender(StreamBridge streamBridge, EventProperty eventProperties){
        return new ProjectEventMQSender(streamBridge, eventProperties);
    }
}
